package com.example.testone.uiComponents;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.testone.R;


/**
 * @Author: dreamgyf
 * @Data: 2021/6/23
 */
public class WordsListItem extends FrameLayout {

    private ImageView mImageView;

    private TextView mTvTitle;

    private TextView mTvSubtitle;

    private TextView mTvSupport;

    //private WordsSwitch mBtnSwitch;

    private View mContainerNotification;

    private TextView mTvNotificationCount;

    private ImageView mIvArrow;

    public WordsListItem(@NonNull Context context) {
        super(context);
        init(null);
    }

    public WordsListItem(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public WordsListItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        LayoutInflater.from(getContext()).inflate(R.layout.biz_wg_view_item_list, this, true);
        bindView();
        initAttrs(attrs);
    }

    private void bindView() {
        mImageView = findViewById(R.id.iv);
        mTvTitle = findViewById(R.id.tv_title);
        mTvSubtitle = findViewById(R.id.tv_subtitle);
        mTvSupport = findViewById(R.id.tv_support);
        //mBtnSwitch = findViewById(R.id.btn_switch);
        mContainerNotification = findViewById(R.id.container_notification);
        mTvNotificationCount = findViewById(R.id.tv_notification_count);
        mIvArrow = findViewById(R.id.iv_arrow);
    }

    private void initAttrs(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.WordsListItem);
            @DrawableRes int iconRes = typedArray.getResourceId(R.styleable.WordsListItem_icon, -1);
            String title = typedArray.getString(R.styleable.WordsListItem_title);
            String subTitle = typedArray.getString(R.styleable.WordsListItem_subtitle);
            String supportText = typedArray.getString(R.styleable.WordsListItem_supportText);
            boolean showSwitchBtn = typedArray.getBoolean(R.styleable.WordsListItem_showSwitchButton, false);
            int notificationCount = typedArray.getInteger(R.styleable.WordsListItem_notificationCount, -1);
            boolean showArrow = typedArray.getBoolean(R.styleable.WordsListItem_showArrow, false);
            typedArray.recycle();

            if (iconRes != -1) {
                setIcon(iconRes);
            }
            setTitle(title);
            setSubtitle(subTitle);
            setSupportText(supportText);
            if (showSwitchBtn) {
                showSwitchButton();
            }
            if (notificationCount != -1) {
                setNotificationCount(notificationCount);
            }
            showArrow(showArrow);
        }
    }

    public void setIcon(@DrawableRes int resId) {
        showIcon(true);
        mImageView.setImageResource(resId);
    }

    public void setIcon(Drawable drawable) {
        showIcon(true);
        mImageView.setImageDrawable(drawable);
    }

    public void showIcon(boolean show) {
        mImageView.setVisibility(show ? VISIBLE : GONE);
    }

    public void setTitle(CharSequence title) {
        mTvTitle.setText(title);
    }

    public void showSubtitle(boolean show) {
        mTvSubtitle.setVisibility(show ? VISIBLE : GONE);
    }

    public void setSubtitle(CharSequence subTitle) {
        showSubtitle(!TextUtils.isEmpty(subTitle));
        mTvSubtitle.setText(subTitle);
    }

    public void showArrow(boolean show) {
        mIvArrow.setVisibility(show ? VISIBLE : GONE);
    }

    public void setSupportText(CharSequence supportText) {
        hideRightWidget();
        if (!TextUtils.isEmpty(supportText)) {
            mTvSupport.setVisibility(VISIBLE);
            mTvSupport.setText(supportText);
        }
    }

    public void showSwitchButton() {
        hideRightWidget();
        //mBtnSwitch.setVisibility(VISIBLE);
    }

    public void setNotificationCount(int count) {
        hideRightWidget();
        if (count > 0) {
            mContainerNotification.setVisibility(VISIBLE);
            mTvNotificationCount.setText(String.valueOf(count));
        }
    }

    public void hideRightWidget() {
        mTvSupport.setVisibility(GONE);
        //mBtnSwitch.setVisibility(GONE);
        mContainerNotification.setVisibility(GONE);
    }

    public boolean isSwitchButtonChecked() {
        //return mBtnSwitch.isChecked();
        return true;
    }
}
